package com.peace.hospital.controller;

import com.alibaba.fastjson.JSON;
import com.peace.hospital.entity.hospitalization.entity.*;
import com.peace.hospital.entity.hospitalization.entity.dto.InpatientmessageDto;
import com.peace.hospital.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/book")
public class BookController {
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("people")
    public void initBinderPeople(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("people.");
    }
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("staffinfo")
    public void initBinderStaffinfo(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("staffinfo.");
    }
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("doctoradvice")
    public void initBinderPatientinformation(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("doctoradvice.");
    }
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("department")
    public void initBinderDepartment(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("department.");
    }
    // 绑定变量名字和属性，参数封装进类
    @InitBinder("position")
    public void initBinderPosition(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("position.");
    }

    @Autowired
    private IPeopleService ipeopleService;

    @Autowired
    private IDepartmentService iDepartmentService;

    @Autowired
    private IDoctoradviceService iDoctoradviceService;

    @Autowired
    private IInpatientmessageService iInpatientmessageService;


    @Autowired
    private ISckbedService iSckbedService;

    @Autowired
    private IStaffinfoService iStaffinfoService;




    /**
     * 显示所有科室列表
     *已检查
     */
    @RequestMapping("/getdeparement")
    public String allDepartment() {
         return JSON.toJSONString(new ResponseBean(200, "success", JSON.toJSONString(iDepartmentService.getAll())));
    }


    /**
     * 选择科室动态显示职员
     *已检查
     * @param department
     * @param position
     * @return
     */
    @RequestMapping(value = "/displayStaffByDepartment",method = RequestMethod.GET)
    public String selectStaff(@ModelAttribute("deparement") Department department, @ModelAttribute("position") Position position) {
        return JSON.toJSONString(new ResponseBean(200, "success",iStaffinfoService.selectStaffByDepid(department, position)));
    }

    /**

     * @return
     * @description 添加住院信息，先判断当前人员是否有住院，再判断是否有当前
     */
    @RequestMapping("/insertInpatientMessage")
    public String insertInpatientMessage(
            @ModelAttribute("people") People people, @ModelAttribute("inpatientmessage") Inpatientmessage inpatientmessage,
            @ModelAttribute("staffinfo") Staffinfo staffinfo) {
        inpatientmessage.setSatid(staffinfo.getStaid());
      InpatientmessageDto dto = new InpatientmessageDto(people,inpatientmessage,staffinfo);
        People p = ipeopleService.selectPeopleByPeople(dto);
       if (p == null) {
            //添加病人信息
            ipeopleService.insertPeople(dto);
            p = ipeopleService.selectPeopleByPeople(dto);
        }
        dto.setPeople(p);
        inpatientmessage.setPid(dto.getPeople().getPid());
     //   System.out.println(inpatientmessage);
//        System.out.println(dto);
//        System.out.println(iInpatientmessageService.selectInpatientMessage(dto));
        if (iInpatientmessageService.selectInpatientMessage(dto) != null) {

          //  return "1";
            if (iInpatientmessageService.insertInner(inpatientmessage)) {

                    return JSON.toJSONString(new ResponseBean(200, "success"));

            }
        }

        return JSON.toJSONString( new ResponseBean(401, "fail"));

    }

    /**
     * 获取当前科室所有空病房 已检查
     * @param department
     * @return
     */
    @RequestMapping("/displayFullSckbed")
    public  String displayFullSckbed(@ModelAttribute("deparement") Department department){

        return JSON.toJSONString(new ResponseBean(200,"success",iSckbedService.selectEmptySckbed(department)));
    }

    /**
     * 显示所有住院病人操作
     *
     * @return
     */
    @RequestMapping(value = "/displayAll",method = RequestMethod.GET)
    public String displayAllInner(@RequestBody Department department) {
        System.out.println(department);
        System.out.println(iInpatientmessageService.getAll(department));
        if (iInpatientmessageService.getAll(department)!=null){
            return JSON.toJSONString(new ResponseBean(200,"success",iInpatientmessageService.getAll(department)));
        }
        return JSON.toJSONString(new ResponseBean(402,"fail"));

    }

    /**
     * 出院操作
     * @return
     */
    @RequestMapping("/updateOut")
    public String updateOut(@ModelAttribute("inpatientmessage") Inpatientmessage inpatientmessage) {

        System.out.println(inpatientmessage);
        if (iInpatientmessageService.updateOut(inpatientmessage)) {
            return JSON.toJSONString(new ResponseBean(200, "success"));
        }
        return JSON.toJSONString( new ResponseBean(401, "fail"));
    }

    /**
     * 显示当条住院信息
     *
     * @param inpatientmessage - 前台显示的数据
     * @return
     */
    @RequestMapping("/displayOneMessage")
    public String displayOneMessage(@ModelAttribute("inpatientmessage") Inpatientmessage inpatientmessage,
                                    @ModelAttribute("people") People people,
                                    @ModelAttribute("sckbed") Sckbed sckbed,
                                    @ModelAttribute("satffinfo") Staffinfo staffinfo){
        InpatientmessageDto dto = new InpatientmessageDto(people,inpatientmessage,staffinfo,sckbed);
        return JSON.toJSONString(new ResponseBean(200,"success",dto));
    }

    /**
     * 修改住院信息
     * @return
     */
    @RequestMapping("/updateNewMessage")
    public String updateNewMessage(@ModelAttribute("inpatientmessage") Inpatientmessage inpatientmessage){
    //    System.out.println(inpatientmessage);
        if (iInpatientmessageService.updateNewMessage(inpatientmessage)==1){
            return JSON.toJSONString( new ResponseBean(200,"success",inpatientmessage));
        }
        return JSON.toJSONString( new ResponseBean(402,"fail"));
    }

    /**
     * 添加医嘱
     * @param doctoradvice
     * @return
     */
    @RequestMapping("/insertDoctorAdvice")
    public String insertDoctorAdvice(@ModelAttribute("doctoradvice") Doctoradvice doctoradvice){
        System.out.println(doctoradvice);
        return JSON.toJSONString(new ResponseBean(200,"success",iDoctoradviceService.insertdoctorAdvice(doctoradvice)));
    }

    /**
     * 搜索医嘱
     * @param people
     * @return
     */
    @RequestMapping("/selectDoctorAdvice")
    public String selectDoctorAdvice(@ModelAttribute("people") People people){
        System.out.println(people);
        return JSON.toJSONString(new ResponseBean(200,"success",iDoctoradviceService.selectAdvice(people)));
    }

    /**
     * 删除医嘱（修改医嘱状态）
     * @param doctoradvice
     * @return
     */
    @RequestMapping("/deleteAdvice")
    public String deleteAdvice(@ModelAttribute("doctoradvice") Doctoradvice doctoradvice){
        System.out.println(doctoradvice);
        return JSON.toJSONString(new ResponseBean(200,"success",iDoctoradviceService.deleteDoctorAdvice(doctoradvice)));
    }

    /**
     * 显示催款信息
     * 已检查
     * @return
     */
    @RequestMapping("displayDebts")
    public String displayDebts(){
        return JSON.toJSONString(new ResponseBean(200,"success",iInpatientmessageService.selectDebts()));
    }
}

